<!--
 * @Descripttion: 活动发布权限
 * @Author: xiao li
 * @Date: 2020-11-24 09:26:51
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-15 14:23:42
-->
<template>
    <div>
        <top-nav></top-nav>
        <div class="page-main">
          <lb-tips>
            <p>门店添加活动发布权限之后，可以在手机端自主发布活动，门店管理员/员工自主审核，适用于加盟店自主管理参与活动</p>
            <p>未添加到该页的，默认不具有发布动态的权限，添加到该页面的，由开关去决定是否具有手机端发布活动的权限</p>
          </lb-tips>
          <el-row class="page-top-operate">
            <lb-button type='primary' @click="toShowDialog" icon='el-icon-plus'>添加发布权限</lb-button>
          </el-row>
          <div class="page-search-form">
            <el-form @submit.native.prevent :inline="true" :model="searchForm" ref="searchForm">
              <el-form-item label="门店名称" prop="title">
                <el-input placeholder="请输入门店名称" v-model="searchForm.title"></el-input>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary"  icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)" >{{$t('action.search')}}</lb-button>
                <lb-button size='medium' icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')" >{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </div>

          <el-table
              v-loading='loading'
              :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
              style="width: 100%">
                <el-table-column
                  prop="title"
                  label="门店名称">
                </el-table-column>
                <el-table-column
                  prop="store_type"
                  label="门店类型">
                    <template slot-scope="scope">
                      <div>{{storeType[scope.row.store_type]}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                  prop="status"
                  label="发布权限">
                    <template slot-scope="scope">
                      <el-switch
                      v-model="scope.row.status"
                      :active-value="1"
                      :inactive-value="0"
                      @change="updateItem(scope.row.id,scope.row.status)"
                      ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                  prop="create_time"
                  label="创建时间">
                  <template slot-scope="scope">
                      <p>{{scope.row.create_time | handleTime(1)}}</p>
                      <p>{{scope.row.create_time | handleTime(2)}}</p>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="status"
                  label="操作">
                    <template slot-scope="scope">
                      <div class="table-operate">
                        <lb-button type='danger' size='mini' plain @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                      </div>
                    </template>
                </el-table-column>
          </el-table>
          <lb-page
            :batch='false'
            :page='searchForm.page'
            :pageSize='searchForm.limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
          </lb-page>
          <el-dialog
            title="新增发布权限"
            :visible.sync="showDialogForm"
            width="480px"
            center>
            <el-form @submit.native.prevent :model="dialogForm" :rules="dialogFormRules" ref='dialogForm'  label-width="80px">
              <el-form-item label="选择门店" prop="store_id">
                <el-cascader
                v-model="dialogForm.store_id"
                :options="storeOptions"
                :props="{ checkStrictly: false, value:'id',label:'title'}"
                clearable></el-cascader>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="toShowDialog">{{$t('action.cancel')}}</el-button>
              <el-button type="primary" @click="submitFormInfo('dialogForm')">{{$t('action.comfirm')}}</el-button>
            </span>
          </el-dialog>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      storeOptions: [],
      storeType: {0: '直营店', 1: '加盟店'},
      searchForm: {
        page: 1,
        limit: 10,
        title: ''
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      showDialogForm: false,
      dialogForm: {
        store_id: [],
        status: 1
      },
      dialogFormRules: {
        store_id: {required: true, type: 'array', message: '请选择门店', trigger: 'blur'},
        status: {required: true}
      }
    }
  },
  activated () {
    this.getTableDataList()
    this.getStoreInfo()
  },
  methods: {
    async getStoreInfo () {
      let {code, data} = await this.$api.overlordAuthStoreSelect()
      if (code !== 200) return
      this.storeOptions = [{
        id: 0,
        title: '直营店',
        children: data[0]
      }, {
        id: 1,
        title: '加盟店',
        children: data[1]
      }]
    },
    toShowDialog (item = {}) {
      if (!item.id) {
        item.status = 1
      }
      for (let key in this.dialogForm) {
        this.dialogForm[key] = item[key]
      }
      this.showDialogForm = !this.showDialogForm
    },
    async submitFormInfo (name) {
      let flag = false
      this.$refs[name].validate(valid => {
        if (valid) {
          flag = true
        }
      })
      if (!flag) return
      let param = JSON.parse(JSON.stringify(this.dialogForm))
      param.store_id = param.store_id[1]
      let {code} = await this.$api.overlordAuthAdd(param)
      if (code !== 200) return
      this.$message.success(this.$t('tips.successSub'))
      this.toShowDialog()
      await Promise.all([this.getTableDataList(), this.getStoreInfo()])
    },
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.overlordAuthList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method: 上下架/删除
     */
    async updateItem (id, status) {
      let {code} = await this.$api.overlordAuthUpdate({id, status})
      if (code === 200) {
        this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
        if (status !== -1) return
        this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
        await Promise.all([this.getTableDataList(), this.getStoreInfo()])
      } else {
        if (status === -1) return
        this.getTableDataList()
      }
    },
    /**
     * @method: 删除
     * @param {*} id
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-cascader{
  width: 300px;
}
.el-input{
  width: 200px;
}
</style>
